#!/bin/bash

STEMP0=`find ./css/ -type f -printf "%p\n" | grep -v "svn" | grep -v ".swp" | grep -i -v "service"`
STEMP1=`find ./img/ -type f -printf "%p\n" | grep -v "svn" | grep -v ".swp" | grep -i -v "service"`
STEMP2=" \
./index.release.html \
./js/build/out/app.js \
./js/build/out/lv.js"

SRC=`echo $STEMP0 $STEMP1 $STEMP2`

for file in $FILES; do
	if [ -f $file ]; then
		echo "Copying file: $file"
	else
		echo "Error: file $file not found"
		 exit 0;
	fi 
done

echo "** Synchronize SVN Application with local web $1 **"

echo -n "Detecting configuration ... "
if [ -n "$1" ]; then
	echo "OK"
else 
	echo "failed"
	echo "ERROR: no target destination set. Set target destination first and restart this tool"
	exit -1
fi

echo -n "Detecting mode ... "
if [ -n "$2" ]; then
	echo "OK"
	if [ "${2}" == "purge" ]; then
		rm -Rf $1
		echo "INFO: Purging the target folder first"
	fi
else 
	echo "failed"
	echo "WARN: mode not set, assuming no purge and skipping purging."
fi

for file in $SRC; do
	NF=`echo "${file}" | sed -e 's/^..//'`
	TARGET="$1/$NF"
	echo -n "copying $file ... "
	echo $TARGET
	if [ ! -d `dirname $TARGET` ]; then
		mkdir -m 755 -p $(dirname $TARGET)
	fi
	cp -fp "${file}" $TARGET
	if [ -f "$TARGET" ]; then
		echo -n " "
	else
		echo " Copying failed"
		echo "Check the destination path $1 and permission to write there"
		exit -1
	fi
done

echo " OK"
echo "Finished. SVN project files copied into $1 location"
